package com.itheima.dbsharding.month.tool;

import org.springframework.util.StringUtils;

import java.lang.management.ManagementFactory;
import java.math.BigDecimal;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.regex.Pattern;

/**
 * @author jiangta
 * @version 1.0
 * @date 2020/5/8 1:23 下午
 */
public class GenericTool {

    private static final String EMAIL_FORMAT =" /^\\w+([\\.-]?\\w+)*@\\w+([\\.-]?\\w+)*(\\.\\w{2,3})+$/";
    public static long getWorkId(){
        String name = ManagementFactory.getRuntimeMXBean().getName();
        long workId =Long.valueOf(name.split("@")[0]);
        return  workId;
    }


    /**
     * 通过时间戳获取时间
     * @param timestamp
     * @return
     */
    public static LocalDateTime getLocalDate(String timestamp){
        Instant startInstant = Instant.ofEpochMilli(Long.valueOf(timestamp));
        LocalDateTime localDate = LocalDateTime.ofInstant(startInstant, ZoneId.systemDefault());
        return localDate;
    }

    public static boolean isEmail(String email){
        if(StringUtils.isEmpty(email)){
            return false;
        }
        return Pattern.matches(EMAIL_FORMAT,email);
    }

    public static void main(String[] args) {
        BigDecimal bigDecimal = new BigDecimal(0.03);
        BigDecimal bigDecimal2 = new BigDecimal(0.02+"");
        System.out.println(bigDecimal.subtract(bigDecimal2));
//        int number = 20;
//        int result = 1;
//        for(int i=number;i>1;i--){
//            result = result*i;
//        }
//        String swp = String.valueOf(result);
//        char[] chars = swp.toCharArray();
//        int num = 0;
//        for(int i = chars.length-1;i>0;i--){
//            if(chars[i]=='0'){
//                num++;
//            }else{
//                break;
//            }
//        }
//        System.out.println("输入的值为"+number);
//        System.out.println("n的阶乘为"+result);
//        System.out.println("结尾的零的个数"+num);
//        int a = 10;
//        int b = 2;
//        int test = 4;
//        System.out.println(test<<1);
//        int sum = Add2(a,b);
    }
    static int Add2(int a, int b)
    {
        if(0 == b){
            return a;
        }
        int cxor = a^b;
        int cand = a&b;
        return Add2(cxor, cand<<1);
    }
}
